package com.onlinescxhz.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.onlinescxhz.demo.pojo.GoodsCategory;
import com.onlinescxhz.demo.service.GooodsCategoryService;
import com.onlinescxhz.demo.service.GooodsCategoryServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/goodscategory")
public class GooodsCategoryController {

    @Resource
    private GooodsCategoryService gooodsCategoryService;
    @Resource
    private GooodsCategoryServiceImpl gooodsCategoryServiceimpl;
    @GetMapping("/page")
    public IPage<GoodsCategory> findpage(@RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize,
                                        @RequestParam String category,
                                        @RequestParam Integer uid){
//        pageNum=(pageNum-1)*pageSize;
////        List<GoodsCategory> data=gooodsCategoryService.selectPagemy(pageNum,pageSize);
////        Integer total=gooodsCategoryService.selectTot();
////        Map<String, Object> res= new HashMap<>();
////        res.put("data",data);
////        res.put("total",total);
////        return res;
        IPage<GoodsCategory> page= new Page<>(pageNum,pageSize);
        QueryWrapper<GoodsCategory> queryWrapper= new QueryWrapper<>();
        if(!"".equals(category)) {
            queryWrapper.like("category", category);
            queryWrapper.eq("uid", uid);
        }
        return gooodsCategoryServiceimpl.page(page,queryWrapper);
    }
    @PostMapping("/statuss")
    public boolean updatestutass(@RequestBody GoodsCategory goodsCategory){
        System.out.println(goodsCategory);
        QueryWrapper<GoodsCategory> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",goodsCategory.getId());
        return gooodsCategoryServiceimpl.update(goodsCategory,queryWrapper);
    }
    @PostMapping("/update")
    public boolean updategoodsCategory(@RequestBody GoodsCategory goodsCategory){
        QueryWrapper<GoodsCategory> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("id",goodsCategory.getId());
        return gooodsCategoryServiceimpl.update(goodsCategory,queryWrapper);
    }
    @GetMapping("/delete")
    public boolean deletegoodsCategory(@RequestParam Integer id){
        return gooodsCategoryServiceimpl.removeById(id);
    }
    @PostMapping("/add")
    public boolean add(@RequestBody GoodsCategory goodsCategory){
        goodsCategory.setId(0);
        System.out.println(goodsCategory);
        return gooodsCategoryServiceimpl.save(goodsCategory);
    }

    /**
     * by 商户id 搜索产品分类
     * @return
     */
    @PostMapping("/selectlist")
    public List<GoodsCategory> selectall(@RequestBody Integer uid){
        QueryWrapper<GoodsCategory> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return gooodsCategoryServiceimpl.list(queryWrapper);
    }

}
